package com.akk.service;

import com.akk.bean.Employee;
import com.akk.bean.EmployeeExample;
import com.akk.bean.EmployeeExample.Criteria;
import com.akk.dao.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;

    //員工保存
    public void saveEmp(Employee employee) {
        employeeMapper.insertSelective(employee);
    }
//
    // 
    /**
     * 檢驗用户名是否重复
     * @param empName
     * @return true:(==0)代表當前姓名可用   false:(>0)姓名不可用
     */
    public boolean checkUser(String empName) {
        EmployeeExample employeeExample = new EmployeeExample();
        Criteria criteria = employeeExample.createCriteria();
        criteria.andEmpNameEqualTo(empName);
        long count = employeeMapper.countByExample(employeeExample);
        return count == 0;
    }

    // 根据员工id查询员工信息
    public Employee getEmp(Integer id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        System.out.println("EmployeeService的根据员工id查询员工信息(更新前取得員工訊息)"+employee);
        return employee;
    }
 // 获取所有员工信息
	public List<Employee> getAll() {
		System.out.println("走到EmployeeService 的getAll()");
		return employeeMapper.selectByExampleWithDept(null);
	}
	
	

    // 员工更新
    public void updateEmp(Employee employee) {
        System.out.println("更新裡面"+employee);
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    // 根据id删除员工
    public void deleteEmp(Integer id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    public void deleteBatch(List<Integer> ids) {
        EmployeeExample example = new EmployeeExample();
        Criteria criteria = example.createCriteria();
        //SQL: delete from xxx where emp_id in(1,2,3);
        criteria.andEmpIdIn(ids);
        employeeMapper.deleteByExample(example);
    }
}